//
//  BindCardViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddBindCardViewController.h"
#import "JddViewControllerManager.h"

@interface JddBindCardViewController ()

@end

@implementation JddBindCardViewController

@synthesize realNameTextField;
@synthesize IDCardTextField;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddBindCardViewController);

-(void)QueryUserInfoOperationResult:(NSData *)result{
    if (result) {
        if ([JddDataParser parseUserInfoResult:result]) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_CARD_INFO 
                                                                 fromController:self
                                                                     withObject:nil
                                                                    withObject2:nil];
        }
    }
}

- (void)UpdateUserBaseInfoOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    
    if (result) {
        if ([JddDataParser parseUserInfoResult:result]) {
            JddQueryUserInfoOperation *operation = [[JddQueryUserInfoOperation alloc] init];
            operation.delegate = self;
            [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];                        
        }else{
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
    }
}

-(IBAction)handleSingleTapGesture{
    [realNameTextField resignFirstResponder];
    [IDCardTextField resignFirstResponder];
    
}


- (IBAction)nextButtonClicked:(id)sender{
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([0-9]{17})([0,1,2,3,4,5,6,7,8,9,x,X])$"];
    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([0-9]{15})$"];
    if (![IDCardTextField.text length]) {
        [JddCommonFunction messageBoxWithMessage:@"请输入身份证号！"];
        return;
    }
    else {
        if ([IDCardTextField.text length]==18){
            if (![pred evaluateWithObject:IDCardTextField.text]) {
                [JddCommonFunction messageBoxWithMessage:@"请输入正确的身份证号！"];
                return;
            }
        }
        else if ([IDCardTextField.text length]==15) {
            if (![pred2 evaluateWithObject:IDCardTextField.text]) {
                [JddCommonFunction messageBoxWithMessage:@"请输入正确的身份证号！"];
                return;
            }
        }else {
            [JddCommonFunction messageBoxWithMessage:@"请输入正确的身份证号！"];
            return;
        }
    }
    if (isNeedUpdate) {
        if (![realNameTextField.text length] ||
            ![IDCardTextField.text length]) {
            [JddCommonFunction messageBoxWithMessage:INFORMATION_NOT_INTACT];
            
            return;
        }
        
        [JddCommonFunction setWaitingViewHidden:NO fromController:self];
        
        JddUpdateUserBaseInfoOperation *operation = [[JddUpdateUserBaseInfoOperation alloc] init];
        operation.delegate = self;
        operation.realName =realNameTextField.text;
        operation.idcard = IDCardTextField.text;
        operation.mobile = @"";
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
    else {
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_CARD_INFO 
                                                             fromController:self
                                                                 withObject:nil
                                                                withObject2:nil];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

//    [self.navigationItem setTitle:PAGE_BIND_CARD];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_BIND_CARD];
        customLab.textAlignment = NSTextAlignmentCenter;
        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
//        //self.navigationItem.hidesBackButton = YES;
        isNeedUpdate = YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"BindUserInfo"];
    [MobClick event:@""];

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"BindUserInfo"];
    
    realNameTextField.text = @"";
    IDCardTextField.text = @"";
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
